package com.zhongzhi.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.dao.TypeDao;
import com.zhongzhi.service.TypeService;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class TypeServiceImpl implements TypeService{

	@Autowired
	private TypeDao typeDao;
	@Autowired
	private MeasureDao measureDao;
	//增
	@Override
	public Bo insert(Type type) {
		log.info("后端业务层:机型管理:新增机型:"+type);
		//参数校验
		if (type==null || type.equals("")) {
			return new Bo(false, 400, "机型名称不能为空", 0,null);
		}else {
			int a = typeDao.selectByNameSize(type.getTypeName());
			log.info("后端业务层:机型管理:新增机型数量:"+a);
			if (a>0) {
				return new Bo(false, 400, "机型名称重复",0,null);
			}else {
				JsonResult insertServer = insertServer(type);
				if (insertServer.getCode()!=200) {
					log.error("后端:机型:新增失败:"+insertServer.getData().toString());
					return new Bo(false, 500, "存储数据失败", 0,null);
				}else {
					log.info("后端业务层:机型管理:新增成功:"+type);
					return new Bo(false, 200, "存储数据成功", 1,0);
				}
			}
		}
	
	}

	//改
	@Override
	public Bo update(Type type) {
		log.info("后端业务层:机型管理:修改机型:"+type);
		//参数校验
		if (type==null || type.equals("")) {
			return new Bo(false, 400, "机型名称不能为空", 0,null);
		}else {
			List<Type> a = typeDao.selectByName(type.getTypeName());
			log.info("后端业务层:机型管理:修改机型:查询机型集合:"+a);
			if (a.size()!=0) {
				return new Bo(false, 400, "机型名称重复",0,null);
			}else {
				JsonResult insertServer = updateServer(type);
				if (insertServer.getCode()!=200) {
					log.error("后端:机型:修改失败:"+insertServer.getData().toString());
					return new Bo(false, 500, "修改数据失败", 0,null);
				}else {
					log.info("后端业务层:机型管理:修改成功:"+type);
					return new Bo(false, 200, "修改数据成功", 1,0);
				}
			}
		}
		
	}

	//删
	@Override
	public Bo delete(Integer id) {
		log.info("后端业务层:机型管理:删除机型:"+id);
		if (id<=0) {
			return new Bo(false, 400, "机型id不正确", 0,null);
		}else {
			int i = 0;
			try {
				i = measureDao.selectTypeId(id);
				log.info("后端业务层:机型管理:删除机型:查询机型下的数据条数:"+i);
			} catch (Exception e) {
				log.error("后端业务层:机型管理:删除机型:查询机型下的数据条数失败:"+e);
				return new Bo(false, 500, "由于查询该机型下属的测试数据失败,故此次删除业务失败", 1,e);
			}
			if (i>0) {
				return new Bo(false, 400, "由于该机型拥有测试数据,故无法删除", 0,null);
			}else {
				JsonResult insertServer = deleteServer(id);
				if (insertServer.getCode()!=200) {
					log.error("后端:机型:删除失败:"+insertServer.getData().toString());
					return new Bo(false, 500, "删除数据失败", 0,null);
				}else {
					log.info("后端业务层:机型管理:删除成功:");
					return new Bo(false, 200, "删除数据成功", 1,0);
				}
			}
		}
	}

	//分页查询
	@Override
	public Bo select(Integer pages, Integer line) {
		log.info("后端业务层:机型管理:分页查询:pages="+pages+"line="+line);
		//数据校验
		if (pages==null) {
			pages=1;
		}
		if (line==null) {
			line=20;
		}	
		int a = (pages-1)*line;
		try {
			List<Type> select = typeDao.select(a,line);
			log.info("后端业务层:机型管理:分页查询:pages="+pages+"line="+line+"结果:"+select);
			return new Bo(true, 200, "查询机型成功",select.size(),select);
		} catch (Exception e) {
			log.error("后端业务层:机型管理:分页查询失败:"+e);
			return new Bo(false, 500, "查询机型失败", 1,e);
		}
		
	}

	//获取总记录数
	@Override
	public Bo selectSize() {
		log.info("后端业务层:机型管理:获取记录总数");
		try {
			int size = typeDao.selectSize();
			log.info("后端业务层:机型管理:获取记录总数结果:"+size);
			return new Bo(true, 200, "查询设备数量成功",1,size);
		} catch (Exception e) {
			log.error("后端业务层:机型管理:获取记录总数结失败:"+e);
			return new Bo(false, 500, "查询设备数量失败", 1,e);
		}
	}

	//按照指定id查询
	@Override
	public Bo selectById(Integer id) {
		log.info("后端业务层:机型管理:根据id查询数据:"+id);
		if (id<=0) {
			return new Bo(false, 400, "查询设备id编号不能小于0", 0,null);
		}else {
			try {
				Type type = typeDao.selectById(id);
				log.info("后端业务层:机型管理:根据id查询数据:"+id+"结果:"+type);
				return new Bo(true, 200, "id查询设备数量成功",1,type);
			} catch (Exception e) {
				log.error("后端业务层:机型管理:根据id查询数据失败:"+e);
				return new Bo(false, 500, "id查询设备数量失败", 1,e);
			}
		}
	}

	@Override
	public Bo selectByNameSize(String typeName) {
		log.info("后端业务层:机型管理:根据机型名字查询机型数量:"+typeName);
		if (typeName==null || typeName.equals("")) {
			return new Bo(false, 400, "名称不能为空", 0,null);
		}else {
			try {
				int a = typeDao.selectByNameSize(typeName);
				log.info("后端业务层:机型管理:根据机型名字查询机型数量:"+typeName+"结果:"+a);
				return new Bo(true, 200, "根据机号查询设备数量成功",1,a);
			} catch (Exception e) {
				log.error("端业务层:机型管理:根据机型名字查询机型数量:"+e);
				return new Bo(false, 500, "根据机号查询设备数量失败", 1,e);
			}
		}
	}

	@Override
	public Bo selectByName(String typeName) {
		log.info("后端业务层:机型管理:根据机型名字查询机型:"+typeName);
		List<Type> list= typeDao.selectByName(typeName);
		log.info("后端业务层:机型管理:根据机型名字查询机型:"+typeName+"结果:"+list);
		try {
			return new Bo(true, 200, "根据机型名称查询id成功", 1, list);
		} catch (Exception e) {
			log.error("端业务层:机型管理:根据机型名字查询机型:"+e);
			return new Bo(false, 500, "根据机型名称查询id失败", 1, null);
		}
		
	}

	@Override
	public Bo webinsert(Type type) {
		log.info("后端:机型:服务器新增:"+type);
		try {
			int a = typeDao.insert(type);
			log.info("后端:机型:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:机型:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webupdate(Type type) {
		log.info("后端:机型:服务器新增:"+type);
		try {
			int a = typeDao.update(type);
			log.info("后端:机型:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:机型:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webdelete(Integer id) {
		log.info("后端:机型:服务器新增:"+id);
		try {
			int a = typeDao.delete(id);
			log.info("后端:机型:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:机型:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webselect() {
		log.info("后端:机型:服务器新增:");
		try {
			ArrayList<Type> list =typeDao.webselect();
			log.info("后端:机型:服务器新增成功:"+list);
			return new Bo(true, 200, "服务器新增成功",1,list);
		} catch (Exception e) {
			log.error("后端:机型:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}

	private JsonResult insertServer(Type type) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/type/insert?id="+type.getId()
		+"&typeName="+type.getTypeName()
		+"&remarks=" + type.getRemarks();
		try {
			log.info("后端:机型:同步新增:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:机型:同步新增:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = typeDao.insert(type);
				log.info("后端:机型:同步新增:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:机型:同步新增:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
		
		
	}

	private JsonResult updateServer(Type type) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/type/update?id="+type.getId()
		+"&typeName="+type.getTypeName()
		+"&remarks=" + type.getRemarks();
		try {
			log.info("后端:机型:同步修改:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:机型:同步修改:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = typeDao.update(type);
				log.info("后端:机型:同步修改:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:机型:同步修改:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}

	private JsonResult deleteServer(Integer id) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/type/delete?id="+id;
		try {
			log.info("后端:机型:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:机型:同步删除:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = typeDao.delete(id);
				log.info("后端:机型:同步删除:"+insert);
				return new JsonResult(200, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:机型:同步删除:"+e2);
				return new JsonResult(500, "", 1, e2);
			}
		}
	}



}
